import java.util.HashMap;

/**
 * 题目链接：https://leetcode.cn/problems/subarray-sums-divisible-by-k/
**/
public class Test {
    public static void main(String[] args) {
        //
    }
}
class Solution {
    public int subarraysDivByK(int[] nums, int k) {
        HashMap<Integer, Integer> hash = new HashMap<>();
        hash.put(0,1);

        int sum = 0;
        int ret = 0;
        for(int x : nums){
            sum += x;//计算当前位置的前缀和
            int number = (sum % k + k) % k;
            ret += hash.getOrDefault(number, 0);
            hash.put(number, hash.getOrDefault(number, 0) + 1);
        }
        return ret;
    }
}
